package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.model.History;
import com.snail.snailvr.utils.Util;
import com.snail.snailvr.widget.FlexibleDividerDecoration;
import com.snail.snailvr.widget.HorizontalDividerItemDecoration;
import com.snail.snailvr.widget.ThumbView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * //TODO 添加baseDividerAdapter
 * 播放历史记录列表
 * Created by snail on 16/4/21.
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder>  implements
        FlexibleDividerDecoration.PaintProvider,
        FlexibleDividerDecoration.VisibilityProvider,
        HorizontalDividerItemDecoration.MarginProvider  {
    private List<History> mHistoryList;
    private LayoutInflater mLayoutInflater;
    private View.OnClickListener mOnClickListener;
    private boolean isTransparent;//item的背景是否透明
    private Context mContext;
    private Paint dividerPaint;
    private int dividerMarginLeft;
    public HistoryAdapter(Context context, List<History> historyList, View.OnClickListener clickListener,boolean isTransparent) {
        mContext=context;
        mHistoryList = historyList;
        mLayoutInflater=LayoutInflater.from(context);
        mOnClickListener=clickListener;
        this.isTransparent=isTransparent;
        dividerPaint = Util.getDividerPaint();
        dividerMarginLeft = context.getResources().getDimensionPixelSize(R.dimen.input_divider_margin_left);
    }

    @Override
    public HistoryAdapter.HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HistoryViewHolder(mLayoutInflater.inflate(R.layout.history_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(HistoryAdapter.HistoryViewHolder holder, int position) {
        History history=getItem(position);
        if(history!=null){
            holder.itemView.setTag(history);
            holder.mVideoThum.setImageUrlAndReUse(history.getPath());
            holder.mVideoTitle.setText(history.getName());
        }
    }

    @Override
    public int getItemCount() {
        return mHistoryList.size();
    }

    public History getItem(int position){
        if(position<getItemCount())
            return mHistoryList.get(position);
        return null;
    }
    @Override
    public int dividerLeftMargin(int position, RecyclerView parent) {
        return dividerMarginLeft;
    }

    @Override
    public int dividerRightMargin(int position, RecyclerView parent) {
        return 0;
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        return dividerPaint;
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        return false;
    }

    class HistoryViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.video_thumb)
        ThumbView mVideoThum;
        @Bind(R.id.video_title)
        TextView mVideoTitle;
        @Bind(R.id.history_item_layout)
        View itemLayout;
        public HistoryViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(mOnClickListener);
            if(isTransparent)
                itemLayout.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
        }
    }
}
